var pathName = window.document.location.pathname;
var locat = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
/*模板自定义添加列*/
function pageaddterm(){
	var trLength =$("button .upbtn").length;
	$("#tbody").append("<tr>"
			+"<td><input type='text' class='form-control ' name='' onchange='input(this);'  />"
			+"<input type='hidden' class='form-control ' name='' value='icon-file-alt'  />"
			+"<input type='hidden' class='form-control ' name=''  value='1' />"
			+"<input type='hidden' class='form-control sort' name='' value='"+trLength+"'  /></td>"
			+"<td>"
			+"		<button type='button' class='btn btn-xs' onclick='deltr(this);' >"
			+"        	<span class='glyphicon glyphicon-trash'  style='color: #63B8FF'></span>"
			+"        </button>"
			+"</td>"
			+"<td>"
			+"		<button type='button' class='btn btn-xs' onclick='upbth(this);' >"
			+"        	<span class='glyphicon glyphicon-chevron-up upbtn'  style='color: #63B8FF'></span>"
			+"        </button>"
			+"</td>"
			+"<td>"
			+"		<button type='button' class='btn btn-xs' onclick='downbtn(this);' >"
			+"        	<span class='glyphicon glyphicon-chevron-down downbtn'  style='color: #63B8FF'></span>"
			+"        </button>"
			+"</td>"
			+"<td>"
			+"		<button type='button' class='btn btn-xs'  onclick='eye(this);'>"
			+"        	<span class='glyphicon glyphicon-eye-open'   style='color: #63B8FF'></span>"
			+"        </button>"
			+"</td>"
			+"<td><button type='button' class='btn btn-xs'  onclick='file(this);'>"
			+"       <span class='glyphicon icon-file-alt'   style='color: #63B8FF'></span>"
			+"      </button>"
			+"</td>"
			+"</tr>");
};

/*修改模板页面根据json字符串解析成列显示*/
function updatepageinitterm(dataStr){
	$.each(dataStr,function(index,item){ 
		 var ix = 0; //查询出来的顺序
		 var inp = "";//文本框HTML代码
		 var iseye="";//是否显示字段
		 var inputtype="";//inputtype 文本框类型
		 var name = "";//类型名字
		 var sort = "";//排序类型 
		 $.each(item,function(i,it){
			 if(ix==0){
				 name=i;
				inp+="<input type='text' class='form-control ' name="+i+" onchange='input(this);' value="+it+"  />";
			 }else{
				 if(ix!=3){
					 inp+="<input type='hidden' class='form-control ' name="+name+" onchange='input(this);' value="+it+"  />";
				 }else{
					 inp+="<input type='hidden' class='form-control sort ' name="+name+" onchange='input(this);' value="+it+"  />";
				 }
				
				 if(ix==2){
					 if(it ==1){
						iseye="<span class='glyphicon glyphicon-eye-open'   style='color: #63B8FF'>";
					 }else{
						iseye="<span class='glyphicon glyphicon-eye-close'></span>";
					 }
				 }else if(ix==1){
					inputtype="<span class='glyphicon "+it+"'   style='color: #63B8FF'></span>";
				 }
			 }
			 if(ix==3){
				 if($("#tbody tr").html()==null){
				 $("#tbody").prepend("<tr>"
							+"<td>"+inp+"</td>"
							+"<td>"
							+"		<button type='button' class='btn btn-xs' onclick='deltr(this);' >"
							+"        	<span class='glyphicon glyphicon-trash'  style='color: #63B8FF'></span>"
							+"        </button>"
							+"</td>"
							+"<td>"
							+"		<button type='button' class='btn btn-xs' onclick='upbth(this);' >"
							+"        	<span class='glyphicon glyphicon-chevron-up upbtn'  style='color: #63B8FF'></span>"
							+"        </button>"
							+"</td>"
							+"<td>"
							+"		<button type='button' class='btn btn-xs' onclick='downbtn(this);' >"
							+"        	<span class='glyphicon glyphicon-chevron-down downbtn'  style='color: #63B8FF'></span>"
							+"        </button>"
							+"</td>"
							+"<td>"
							+"		<button type='button' class='btn btn-xs'  onclick='eye(this);'>"
							+iseye
							+"        </button>"
							+"</td>"
							+"<td><button type='button' class='btn btn-xs'  onclick='file(this);'>"
							+inputtype
							+"      </button>"
							+"</td>"
							+"</tr>");
				 }else{
					 	var $bss=0;
					   $("input.sort").each(function (i) {
							if($(this).val()>it){
								$(this).parents("tr").before("<tr>"
		    							+"<td>"+inp+"</td>"
		    							+"<td>"
		    							+"		<button type='button' class='btn btn-xs' onclick='deltr(this);' >"
		    							+"        	<span class='glyphicon glyphicon-trash'  style='color: #63B8FF'></span>"
		    							+"        </button>"
		    							+"</td>"
		    							+"<td>"
		    							+"		<button type='button' class='btn btn-xs' onclick='upbth(this);' >"
		    							+"        	<span class='glyphicon glyphicon-chevron-up upbtn'  style='color: #63B8FF'></span>"
		    							+"        </button>"
		    							+"</td>"
		    							+"<td>"
		    							+"		<button type='button' class='btn btn-xs' onclick='downbtn(this);' >"
		    							+"        	<span class='glyphicon glyphicon-chevron-down downbtn'  style='color: #63B8FF'></span>"
		    							+"        </button>"
		    							+"</td>"
		    							+"<td>"
		    							+"		<button type='button' class='btn btn-xs'  onclick='eye(this);'>"
		    							+iseye
		    							+"        </button>"
		    							+"</td>"
		    							+"<td><button type='button' class='btn btn-xs'  onclick='file(this);'>"
		    							+inputtype
		    							+"      </button>"
		    							+"</td>"
		    							+"</tr>");
									$bss=1;
									return false;
							}
						});
					   if($bss==0){
						   $("#tbody").append("<tr>"
	    							+"<td>"+inp+"</td>"
	    							+"<td>"
	    							+"		<button type='button' class='btn btn-xs' onclick='deltr(this);' >"
	    							+"        	<span class='glyphicon glyphicon-trash'  style='color: #63B8FF'></span>"
	    							+"        </button>"
	    							+"</td>"
	    							+"<td>"
	    							+"		<button type='button' class='btn btn-xs' onclick='upbth(this);' >"
	    							+"        	<span class='glyphicon glyphicon-chevron-up upbtn'  style='color: #63B8FF'></span>"
	    							+"        </button>"
	    							+"</td>"
	    							+"<td>"
	    							+"		<button type='button' class='btn btn-xs' onclick='downbtn(this);' >"
	    							+"        	<span class='glyphicon glyphicon-chevron-down downbtn'  style='color: #63B8FF'></span>"
	    							+"        </button>"
	    							+"</td>"
	    							+"<td>"
	    							+"		<button type='button' class='btn btn-xs'  onclick='eye(this);'>"
	    							+iseye
	    							+"        </button>"
	    							+"</td>"
	    							+"<td><button type='button' class='btn btn-xs'  onclick='file(this);'>"
	    							+inputtype
	    							+"      </button>"
	    							+"</td>"
	    							+"</tr>");
						}
				 }
			 }
			ix++;
		 });
	 });
}


//删除tr
function deltr(obj){
	  $(obj).parent("td").parent("tr").remove();
	  $("input.sort").each(function (i) {
			$(this).val(i);
		});
}

/*选择input类型*/
function file(obj){
	  var diag = new top.Dialog();
		 diag.Drag=true;
		 diag.Title ="类型设置";
		 diag.URL = locat+"/goodstemplate/inputType?id=&quot;"+$(obj).parent("td").siblings().eq(0).children().eq(1).val()+"&quot;";
		 diag.Width = 400;
		 diag.Height = 224;
		 diag.CancelEvent = function(){ //关闭事件
			 if(diag.innerFrame.contentWindow.document.getElementById('buttype').value==1){
				$(obj).html("<span class='glyphicon "+diag.innerFrame.contentWindow.document.getElementById('inputtype').value+"'   style='color: #63B8FF'></span>");
				$(obj).parent("td").siblings().eq(0).children().eq(1).val(diag.innerFrame.contentWindow.document.getElementById('inputtype').value);
			 }
			diag.close();
		 };
		 diag.show();
	 }


/*input标签的name属性根据输入值自动生成*/
function input(obj){
	$.ajax({
		 url:locat+ '/nodeTemplate/tofirst?value='+$(obj).val(),
         type: 'post',
         success: function (data) {
        	 $(obj).attr("name",data);
        	 $(obj).siblings().attr("name",data);
         }
	});    	
}



//上移
function upbth(obj){
	var $tr = $(obj).parents("tr");
		if ($tr.index() != 0) {
			$tr.prev().before($tr);
			$("input.sort").each(function (i) {
				$(this).val(i);
			});
		}
	}

	//下移
function downbtn(obj) {
		var trLength = $("button .downbtn").length; 
		var $tr = $(obj).parents("tr"); 
		if ($tr.index() != trLength - 1) { 
			$tr.next().after($tr);
			$("input.sort").each(function (i) {
				$(this).val(i);
			});
		}
	}
